package jsp;

import entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 石小俊
 */
@WebServlet("*.session")
public class SessionServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        switch (path) {
            case "/add.session":
                addSession(req, resp);
                break;
            case "/find.session":
                findSession(req, resp);
                break;
            case "/modify.session":
                modifySession(req, resp);
                break;
            case "/remove.session":
                removeSession(req, resp);
                break;
            case "/clean.session":
                clean(req, resp);
                break;
            case "/count.session":
                count(req, resp);
                break;
        }
    }

    protected void count(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取当前保存的访问次数
        Integer count = (Integer) req.getSession().getAttribute("count");
        // 判断是否是第一次访问,如果count的值为null,说明是第一次访问
        if(count == null){
            // 如果是第一次,则设置初始值1
            count = 1;
        } else{
            // 否则在原有基础上+1
            count++;
        }
        req.getSession().setAttribute("count",count);
        req.getRequestDispatcher("/jsp/count.jsp").forward(req,resp);
    }

    protected void addSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        // session中可以存储任意类型的数据
        session.setAttribute("username", "admin");
        // 在session中,其默认时间为半小时
        // 如果其值设置为0,表示永不过期
        // 但是如果session被销毁,数据仍然会丢失
        session.setMaxInactiveInterval(0);
        Emp emp = new Emp();
        emp.setId(1);
        emp.setName("admin");
        emp.setSalary(8000.0);
        session.setAttribute("emp", emp);

        Emp emp2 = new Emp(2, "tom", 9000.0);
        Emp emp3 = new Emp(3, "lucy", 7000.0);
        List<Emp> emps = new ArrayList<>(Arrays.asList(emp, emp2, emp3));
        session.setAttribute("emps", emps);
    }

    protected void findSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println("username:" + username);
        Emp emp = (Emp) session.getAttribute("emp");
        System.out.println(emp);
        List<Emp> emps = (List<Emp>) session.getAttribute("emps");
        for (Emp e : emps) {
            System.out.println(e);
        }
    }

    protected void modifySession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // HttpSession中没有提供修改方法,但是在存储数据的时候,如果key相同,则覆盖原来的
        HttpSession session = req.getSession();
        session.setAttribute("username", "jack");
        Emp emp = new Emp(2, "tom", 9000.0);
        session.setAttribute("emp", emp);
    }

    protected void removeSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // HttpSession提供了两种删除方法
        // 1.删除指定key的数据
        req.getSession().removeAttribute("username");
    }

    protected void clean(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 2.清空session
        // 其本质上不是做的清空数据
        // 而是销毁当前session并创建一个新的session
        req.getSession().invalidate();
    }
}
